"""
Case Type   : 系统内部使用工具
Case Name   : 数据库集群状态为unavailable时进行减容，减容失败
Create At   : 2022/04/11
Owner       : opentestcase018
Description :
    1.停止数据库构造unavailable集群状态
    2.gs_dropnode进行减容：
    ./gs_dropnode -U {usrname} -G {grp_name} -h {hostname}
    3.清理环境
    启动数据库恢复环境
Expect      :
    1.集群unavailable状态构造成功
    2.备机减容失败
    3.环境恢复成功
History     :
    2022-5-17 opentestcase039 优化用例，根据自动化连跑要求，修改用例为在源库上减容
"""
import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro
primary_sh = CommonSH('PrimaryDbUser')
Constant = Constant()


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primary_root_node = Node('PrimaryRoot')
        self.primary_node = Node('PrimaryDbUser')
        self.standby1_root_node = Node('Standby1Root')
        self.standby2_root_node = Node('Standby2Root')
        self.u_name = self.primary_node.ssh_user
        self.ssh_file = os.path.join('/home', self.u_name, 'hostfile')
        num = primary_sh.get_node_num()
        self.assertGreaterEqual(num, 3, "执行失败:节点数量异常，减容不执行")

    def test_gs_dropnode(self):
        text = '----step1:构建集群unavailable状态 expect:成功----'
        self.log.info(text)
        stop_cmd = f'source {macro.DB_ENV_PATH};' \
                   f'gs_om -t stop;' \
                   f'gs_om -t status --detail;'
        self.log.info(stop_cmd)
        check_res = self.primary_node.sh(stop_cmd).result()
        self.log.info(check_res)
        self.assertIn('Unavailable', check_res, text + '构造集群unavailable失败')

        text = '----step2:执行gs_dropnode进行备机减容 expect:失败'
        self.log.info(text)
        drop_cmd = f'gs_dropnode -U {self.u_name} -G {self.u_name} ' \
                   f'-h {self.standby2_root_node.ssh_host}'
        self.log.info(drop_cmd)
        global shell_path
        shell_path = os.path.join('/', 'home', f'{self.u_name}', 'dropnode.sh')
        shell_cmd = f'touch {shell_path};echo -e {drop_cmd} > {shell_path};' \
                    f'chmod 755 {shell_path};cat {shell_path}'
        self.log.info(shell_cmd)
        shell_res = self.primary_root_node.sh(shell_cmd).result()
        self.log.info(shell_res)
        execute_cmd = f'source {macro.DB_ENV_PATH};' \
                      f'{shell_path};'
        self.log.info(execute_cmd)
        drop_res = self.primary_node.sh(execute_cmd).result()
        self.log.info(drop_res)
        check_msg = f'[GAUSS-35806] Current status of cluster is Unavailable'
        self.assertIn(check_msg, drop_res, text + '执行失败')

    def tearDown(self):
        text = "----step3:恢复环境 expect:成功----"
        self.log.info(text)
        start_cmd = f'source {macro.DB_ENV_PATH};' \
                    f'gs_om -t start;' \
                    f'gs_om -t status --detail;'
        self.log.info(start_cmd)
        check_res = self.primary_node.sh(start_cmd).result()
        self.log.info(check_res)
        del_cmd = f"rm -rf {shell_path}"
        self.log.info(del_cmd)
        del_res = self.primary_root_node.sh(del_cmd).result()
        self.log.info(del_res)
        self.assertEqual('', del_res, '文件删除失败')
        self.assertIn('Successfully started', check_res, '数据库启动失败')
        self.log.info(f'----{os.path.basename(__file__)} finish----')
